//
//  DEHomeHeaderView.m
//  DE
//
//  Created by PEIYANG on 16/9/23.
//  Copyright © 2016年 YP. All rights reserved.
//
#define margin 8
#define buttonW 100
#import "DEHomeHeaderView.h"
#import "DEOrderingView.h"
@interface DEHomeHeaderView ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *ADscrollView;
@property (weak, nonatomic) IBOutlet UIScrollView *orderingScrollView;

@property(nonatomic,strong)UIButton *button;
@property (weak, nonatomic)UIPageControl *pageView;

@property (strong, nonatomic)NSTimer *timer;
@end
@implementation DEHomeHeaderView

+ (instancetype)headerView{
    return[[[NSBundle mainBundle]loadNibNamed:@"DEHomeHeaderView" owner:nil options:nil] lastObject];
}


- (void)layoutSubviews{
    [super layoutSubviews];
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self prepareScollView];
        [self preparePageView];
        
        [self setupOrderingScrollView];
    });
}

- (void)setupOrderingScrollView{
    static  NSInteger index = 0;
    for (NSString *imageName in _orderingImages) {

        UIButton *button = [[UIButton alloc]init];
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        button.tag = index;
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        button.yp_width = buttonW;//第二个scrollView中每个图片的宽度
        button.yp_height = 160;
        button.yp_x = (buttonW + margin) * index + margin;
        [_orderingScrollView addSubview:button];
        
        [button addTarget:self action:@selector(orderingBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _button = button;
        index++;
    }
    
    _orderingScrollView.contentSize = CGSizeMake(_orderingImages.count  * (buttonW + margin), 0);
    _orderingScrollView.showsHorizontalScrollIndicator = NO;
    _orderingScrollView.showsVerticalScrollIndicator = NO;
    
}

- (void)orderingBtnClick:(UIButton *)button
{
    NSLog(@"第%zd个按钮被点击",button.tag);
}
- (void)prepareScollView {
    _ADscrollView.delegate = self;
    
    static NSInteger i = 0;
    for (NSString *imageName in _ADImages) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        CGFloat imageX = self.yp_width * i;
        imageView.frame = CGRectMake(imageX, 0, self.yp_width, 80);
        [_ADscrollView addSubview:imageView];
        
        if (i == _ADImages.count - 1) i = 0;
        i++;
        
        
        
    }
    for (int i = 0; i < _ADImages.count; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] init];
        NSString *name = [NSString stringWithFormat:@"img_%02d",i + 1];
        imageView.image = [UIImage imageNamed:name];
        CGFloat imageX = self.yp_width * i;
        imageView.frame = CGRectMake(imageX, 0, self.yp_width, _ADscrollView.yp_height);
        [_ADscrollView addSubview:imageView];
    }
    
    _ADscrollView.contentSize = CGSizeMake(_ADImages.count * self.yp_width, 0);
    _ADscrollView.showsHorizontalScrollIndicator = NO;
    _ADscrollView.pagingEnabled = YES;
    [self addTimer];
    
}

- (void)addTimer{//每2秒调用一次
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
}
-(void)preparePageView {
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat pageW = 100;
    //设置pageView的位置
    UIPageControl *pageView = [[UIPageControl alloc] initWithFrame:CGRectMake((width - pageW) * 0.5, 75, pageW, 4)];
    pageView.numberOfPages = _ADImages.count;
    pageView.currentPageIndicatorTintColor = [UIColor redColor];
    pageView.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageView.currentPage = 0;
    [self addSubview:pageView];
    self.pageView = pageView;
}


-(void)nextImage {
    CGFloat width = _ADscrollView.frame.size.width;
    static  NSInteger index = 0;
    if (index == _ADImages.count - 1) {//当index超出总数时,赋值为零
        index = 0;
    } else {
        index++;
    }
    [_ADscrollView setContentOffset:CGPointMake(index * width, 0)animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat width = _ADscrollView.frame.size.width;
    int index = (_ADscrollView.contentOffset.x + width * 0.5) / width;
    if (index == _ADImages.count) {
        index = 0;
    }
    self.pageView.currentPage = index;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
    self.timer = nil;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addTimer];
}
@end
